﻿using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;

namespace Bookworm.Controls
{
    public class CyclicScrollViewer : ScrollViewer
    {
        public CyclicScrollViewer()
        {
            this.CommandBindings.Add(new CommandBinding(ScrollBar.LineLeftCommand, LineCommandExecuted));
            this.CommandBindings.Add(new CommandBinding(ScrollBar.LineRightCommand, LineCommandExecuted));
        }

        private void LineCommandExecuted(object sender, ExecutedRoutedEventArgs e)
        {
            if (e.Command == ScrollBar.LineLeftCommand)
            {
                if (this.HorizontalOffset == 0)
                    this.ScrollToRightEnd();
                else
                    this.LineLeft();
            }

            if (e.Command == ScrollBar.LineRightCommand)
            {
                if (this.HorizontalOffset == this.ScrollableWidth)
                    this.ScrollToLeftEnd();
                else
                    this.LineRight();
            }
        }
    }
}